package com.ymatou.iapi.godmch.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.GetAdminBindAgentListBean;
import com.ymatou.iapi.godmch.parameter.adminBindAgentBean;
import com.ymatou.iapi.godmch.parameter.approveAgentBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.rejectAgentBean;
import com.ymatou.iapi.godmch.service.adminBindAgentCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getAdminBindAgentListCall;
import com.ymatou.iapi.im.parameter.switchSellerCSSessionBean;
import com.ymatou.iapi.optrade.parameter.req.RejectRefundBillReq;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.model.mch_info;
import com.ymttest.database.model.mch_relation;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.godmch.mch_relationWapper;

import oracle.jdbc.ttc7.v8Odscrarr;
/**
*运营后台添加代理商列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAdminBindAgentList{
	private static GetAdminBindAgentListBean bean;
	private static getAdminBindAgentListCall call;
    agent_typeWapper agentTypeWapper;
	mch_relationWapper relationWapper;
	mch_infoWapper infoWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("运营后台添加代理商列表");
	}

	@Before
	public void caseUp() {
		bean=new GetAdminBindAgentListBean();
		call=new getAdminBindAgentListCall();
		agentTypeWapper=new agent_typeWapper();
		relationWapper=new mch_relationWapper();
		infoWapper=new mch_infoWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private Integer getStatus(Integer status)
	{
		Integer new_status=0;
		switch(status) {
		   case 0:
			   new_status= 1;//1：正常
			   break;
			  
		   case -2:
			    new_status= 2;//2：待审核
			   break;
//		   case -1:
//			   new_status= 0;//3：已删除 未知
		   case -3:
			    new_status= 3;//3：拒绝
			   break;
		}
		return new_status;
	}
	
	private void check_verify() throws JSONException, InterruptedException
	{
		//mch_relation
		Map<String,Object> map=new HashMap<>();
		map.put("parentMchId", bean.getMchId());
		
		Integer total=relationWapper.selectCount(map);
		map.put("pageIndex", bean.getPageIndex());
		map.put("pageSize", bean.getPageSize());
		List<mch_relation> relations=relationWapper.selectby(map);
		
		JSONArray jArray=call.getList();
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals(total, call.getTotal(), "验证返回的总条数Total");
		
		for(int i=0;i<relations.size();i++) {
			JSONObject object=new JSONObject(jArray.get(i).toString());
			mch_relation relation=relations.get(0);
			Logger.comment("agentId:"+relation.getMchId());
			Logger.verifyEquals(object.getInt("agentId"), relation.getMchId(), "验证第"+(i+1)+"条返回的agentId与数据库的mchId一致");
			//mch_info agentTitle
			List<Integer> mchIds=new ArrayList<Integer>();
			mchIds.add(relation.getMchId());
			mch_info  info=infoWapper.selectbyMchIds(mchIds).get(0);
			Logger.verifyEquals(object.getString("agentTitle"), info.getMchTitle(), "验证第"+(i+1)+"条返回的agentTitle与数据库的MchTitle一致");
			
			Logger.verifyEquals(object.getString("mobile"), relation.getApplyMobile(), "验证第"+(i+1)+"条返回的mobile与数据库的ApplyMobile一致");
			Logger.verifyEquals(object.getString("wechat"), relation.getApplyWechat(), "验证第"+(i+1)+"条返回的wechat与数据库的ApplyWechat一致");
			Logger.verifyEquals(object.getString("remark"), relation.getApplyRemark(), "验证第"+(i+1)+"条返回的Remark与数据库的Remark一致");
			Logger.verifyEquals(object.getInt("status"), getStatus(relation.getState()), "验证第"+(i+1)+"条返回的status与数据库的status一致");
			//addTime
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Logger.verifyEquals(object.getString("addTime"),sdf.format(relation.getCreateTime()), "验证第"+(i+1)+"条返回的addTime与数据库的CreateTime一致");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_001() {
		Logger.start(true,"运营后台添加代理商列表-mchId是独立云版本主商户-状态1：正常");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_001_1() {
		Logger.start(true,"运营后台添加代理商列表-mchId是独立云版本主商户-状态2：待审核");
		try {

			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
//	        //审核通过
//			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBean(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
//			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getAdminBindAgentList_001_3() {
		Logger.start(true,"运营后台添加代理商列表-mchId是独立云版本主商户-状态3：已删除(删除暂时不展示)");
		try {

			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			//删除关系
			GodmchManageCallService.InitremovemchForAgent(adminBindAgentBean.getMchId(), adminBindAgentBean.getAgentId());
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_001_4() {
		Logger.start(true,"运营后台添加代理商列表-mchId是独立云版本主商户-状态4：拒绝");
		try {

			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			rejectAgentBean rejectAgentBean=GodmchManageCallService.initRejectAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.rejectAgentCall(rejectAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			
			check_verify();				
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_002() {
		Logger.start(true,"运营后台添加代理商列表-mchId是公共云版本海外渠道商");
		try {
			//商户id
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
            Integer userId = GodmchManageCallService.channl_CHN().getMchId();
            
            adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),userId);
            adminBindAgentCall adminBindAgentCall =GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
            
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			
			check_verify();	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_003() {
		Logger.start(true,"运营后台添加代理商列表-mchId为空");
		try {
			//bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_004() {
		Logger.start(true,"运营后台添加代理商列表-显示第二页-pageIndex=2,pageSize=1");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			Logger.comment("第1个代理");
			//代理商户1
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理1
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过1
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			Logger.comment("第2个代理");
			//代理商户2
			createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			 adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			 adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			 approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			
			Logger.comment("第3个代理");
			//代理商户3
			 createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			 createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			 adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			 adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			 approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(2);
			bean.setPageSize(1);
			call.setData(bean);
			call.callService();			
			
			check_verify();	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_005() {
		Logger.start(true,"运营后台添加代理商列表-显示最后页-pageIndex=2,pageSize=1");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			Logger.comment("第1个代理");
			//代理商户1
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理1
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过1
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			Logger.comment("第2个代理");
			//代理商户2
			createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			 adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			 adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			 approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(2);
			bean.setPageSize(1);
			call.setData(bean);
			call.callService();			
			
			check_verify();			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_006() {
		Logger.start(true,"运营后台添加代理商列表-pageSize大于总条数-pageSize=99");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(1);
			bean.setPageSize(99);
			call.setData(bean);
			call.callService();			
			
			check_verify();		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_007() {
		Logger.start(true,"运营后台添加代理商列表-PageIndex大于总页数-PageIndex=99-返回空");
		try {
			createIndepMchBean createBean=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall=GodmchManageCallService.CreateIndepMchCall(createBean);
			//代理商户
			createIndepMchBean createBean2=GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createCall2=GodmchManageCallService.CreateIndepMchCall(createBean2);
			//运营绑定的代理
			adminBindAgentBean adminBindAgentBean=GodmchManageCallService.initAdminBindAgent(createCall.getmchId(),createCall2.getmchId());
			adminBindAgentCall adminBindAgentCall=GodmchManageCallService.AdminBindAgent(adminBindAgentBean);
	        //审核通过
			approveAgentBean approveAgentBean=GodmchManageCallService.approveAgentBeanV2(adminBindAgentBean.getMchId(),adminBindAgentBean.getAgentId());
			GodmchManageCallService.ApproveAgentCall(approveAgentBean);
			
			bean.setMchId(adminBindAgentBean.getMchId());
			bean.setPageIndex(99);
			bean.setPageSize(1);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(1, call.getTotal(), "验证返回的总条数Total");
			Logger.verifyEquals(0, call.getList().length(), "验证返回的总条数List");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAdminBindAgentList_008() {
		Logger.start(true,"运营后台添加代理商列表-mchId不是运营后台添加的代理-返回空");
		try {
			//渠道1
			UserCreateBean usercreateBean1=DGUserCallService.initSellerUsercreateBean();//初始化创建用户 为什么手机号没有值
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理1
			usercreateBean1=DGUserCallService.initSellerUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchbean);
			//创建代理1
			List<createmchBean> createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new
					createmchBean[createmchList.size()]));
			
			bean.setMchId(userId_channl);
			bean.setPageIndex(1);
			bean.setPageSize(5);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
			Logger.verifyEquals(0, call.getTotal(), "验证返回的总条数Total");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}